package com.whut.googleplay.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.whut.googleplay.utils.UIUtils;
import com.whut.googleplay.utils.ViewUtils;
import com.whut.googleplay.widget.LoadingPage;
import java.util.List;

public abstract class BaseFragment extends Fragment {

    private LoadingPage mContentPage;



    //setContentView
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //当前的view已经有了一个父亲。必须移除
        if(mContentPage == null){
            mContentPage = new LoadingPage(UIUtils.getContext()) {
                @Override
                public View createSuccessView() {
                    return BaseFragment.this.createSuccessView();
                }

                @Override
                public LoadResult Load() {
                    return BaseFragment.this.load();

                }
            };
        }else{
            ViewUtils.removeSelfFromParent(mContentPage);
        }

        return mContentPage;
    }
    /**
     * 检查服务器返回的json数据
     * @param object
     * @return
     */
    protected LoadingPage.LoadResult check(Object object) {
        if (object == null) {
            return LoadingPage.LoadResult.ERROR;
        }
        if (object instanceof List) {
            List list = (List) object;
            if (list.size() == 0) {
                return LoadingPage.LoadResult.EMPTY;
            }
        }

        return LoadingPage.LoadResult.SUCCESS;
    }

    protected abstract LoadingPage.LoadResult load();

    protected abstract View createSuccessView();

    public void show() {
        if (null != mContentPage) {
            mContentPage.show();
        }

    }

}
